<template>
  <div class="app-container">
    
    
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属问卷id" prop="evaluationPaperId" v-show="false">
        <el-input
          v-model="queryParams.evaluationPaperId"
          placeholder="请输入所属问卷id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="问卷项类别id" prop="paperItemCategoryId">
        <el-select v-model="queryParams.paperItemCategoryId" placeholder="请选择问卷" clearable>
          <el-option
            v-for="category in categoryList"
            :key="category.paperItemCategoryId"
            :label="category.paperItemCategoryName"
            :value="category.paperItemCategoryId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="问卷项id" prop="paperItemId">
        <el-input
          v-model="queryParams.paperItemId"
          placeholder="请输入问卷项id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="问卷项标题" prop="paperItemTitle">
        <el-input
          v-model="queryParams.paperItemTitle"
          placeholder="请输入问卷项标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pingjiao:item:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['pingjiao:item:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pingjiao:item:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pingjiao:item:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="itemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="问卷项id" align="center" prop="paperItemId" />
      <!--
      <el-table-column label="问卷项类别id" align="center" prop="paperItemCategoryId" />
      <el-table-column label="所属问卷id" align="center" prop="evaluationPaperId" />
      -->
      <el-table-column label="问卷项标题" align="center" prop="paperItemTitle" />
      <el-table-column label="问题类型" align="center" prop="problemType" />
      <el-table-column label="选项A" align="center" prop="optionA" />
      <el-table-column label="选项B" align="center" prop="optionB" />
      <el-table-column label="选项C" align="center" prop="optionC" />
      <el-table-column label="选项D" align="center" prop="optionD" />
      <el-table-column label="选项E" align="center" prop="optionE" />
      <el-table-column label="分值" align="center" prop="itemPointValue" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pingjiao:item:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pingjiao:item:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改评教问卷项管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="问卷项类别id" prop="paperItemCategoryId" v-show="false">
          <el-input v-model="form.paperItemCategoryId" placeholder="请输入问卷项类别id" />
        </el-form-item>
        <el-form-item label="所属问卷id" prop="evaluationPaperId" v-show="false">
          <el-input v-model="form.evaluationPaperId" placeholder="请输入所属问卷id" />
        </el-form-item>
        <el-form-item label="问卷项标题" prop="paperItemTitle">
          <el-input v-model="form.paperItemTitle" placeholder="请输入问卷项标题" />
        </el-form-item>
        <el-form-item label="选项A" prop="optionA">
          <el-input v-model="form.optionA" placeholder="请输入选项A" />
        </el-form-item>
        <el-form-item label="选项B" prop="optionB">
          <el-input v-model="form.optionB" placeholder="请输入选项B" />
        </el-form-item>
        <el-form-item label="选项C" prop="optionC">
          <el-input v-model="form.optionC" placeholder="请输入选项C" />
        </el-form-item>
        <el-form-item label="选项D" prop="optionD">
          <el-input v-model="form.optionD" placeholder="请输入选项D" />
        </el-form-item>
        <el-form-item label="选项E" prop="optionE">
          <el-input v-model="form.optionE" placeholder="请输入选项E" />
        </el-form-item>
        <el-form-item label="分值" prop="itemPointValue">
          <el-input v-model="form.itemPointValue" placeholder="请输入分值" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listItem, getItem, delItem, addItem, updateItem } from "@/api/pingjiao/item";
import { listCategory } from "@/api/pingjiao/category";

export default {
  name: "Item",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      category: {},
      // 评教问卷项目管理表格数据
      categoryList: [],
      // 评教问卷项管理表格数据
      itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        paperItemId: null,
        paperItemCategoryId: null,
        evaluationPaperId: null,
        paperItemTitle: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    
    const evaluationPaperId = this.$route.params && this.$route.params.evaluationPaperId;
    const paperItemCategoryId = this.$route.params && this.$route.params.paperItemCategoryId;
    this.category.evaluationPaperId = evaluationPaperId;
    this.category.paperItemCategoryId = paperItemCategoryId;
    this.queryParams.evaluationPaperId = evaluationPaperId;
    this.queryParams.paperItemCategoryId = paperItemCategoryId;
    this.getList();
    this.getCategoryList();
    /*
    const category = this.$route.query;
    this.category = category;
    this.itemList = category.paperItemList;
    this.loading = false;
     */
  },
  methods: {
    /** 查询评教问卷项管理列表 */
    getList() {
      this.loading = true;
      listItem(this.queryParams).then(response => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询评教问卷项管理列表 */
    getCategoryList() {
      let queryParams = {
        pageNum: 1,
        pageSize: 10,
        evaluationPaperId: this.category.evaluationPaperId
      };
      console.log(queryParams);
      listCategory(queryParams).then(response => {
        console.log(response);
        this.categoryList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        paperItemId: null,
        paperItemCategoryId: this.category.paperItemCategoryId,
        evaluationPaperId: this.category.evaluationPaperId,
        paperItemTitle: null,
        problemType: null,
        optionA: null,
        optionB: null,
        optionC: null,
        optionD: null,
        optionE: null,
        itemPointValue: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.paperItemId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    
    /**修改缓存
    updateItemList(row){
      console.log(row);
      for (let i = 0; i<this.itemList.length; i++){
        if (row.paperItemId == this.itemList[i].paperItemId){
          this.itemList.splice(i,1,row);
        }
      }
    },
    
    addItemList(row){
      this.itemList.push(row);
    },
    
    deleteItemList(row){
      if (row.length != null){
        for (let j = 0; j < row.length; j++){
          for (let i = 0; i < this.itemList.length; i++){
            if (row[j] == this.itemList[i].paperItemId){
              this.ItemList.splice(i,1);
            }
          }
        }
      }
      else {
        for (let i = 0; i < this.itemList.length; i++) {
          if (row == this.itemList[i].paperItemId) {
            this.itemList.splice(i, 1);
          }
        }
      }
    },
     */
    
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加评教问卷项管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const paperItemId = row.paperItemId || this.ids
      getItem(paperItemId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改评教问卷项管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.paperItemId != null) {
            updateItem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              //this.updateItemList(this.form);
              this.getList();
            });
          } else {
            addItem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              //this.addItemList(response.data);
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const paperItemIds = row.paperItemId || this.ids;
      this.$modal.confirm('是否确认删除评教问卷项管理编号为"' + paperItemIds + '"的数据项？').then(function() {
        return delItem(paperItemIds);
      }).then(() => {
        this.getList();
        //this.deleteItemList(paperItemIds);
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pingjiao/item/export', {
        ...this.queryParams
      }, `item_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
